﻿using JustTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;

namespace JustTest.Admin
{
    public partial class BrowseCategories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public IQueryable<JustTest.Models.Category> GridViewCategories_GetData()
        {
            JustTestDb context = new JustTestDb();

            return context.Categoriess.Include(c=>c.Tests);
        }

        public void GridViewCategories_UpdateItem(int id)
        {
            JustTestDb context = new JustTestDb();
            JustTest.Models.Category category = context.Categoriess.Find(id);
            if (category == null)
            {
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }
            TryUpdateModel(category);
            if (ModelState.IsValid)
            {
                context.SaveChanges();
            }
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewCategories_DeleteItem(int id)
        {
            JustTestDb context = new JustTestDb();
            JustTest.Models.Category category = context.Categoriess.Find(id);
            context.Categoriess.Remove(category);
        }

        protected void ButtonAddCategory_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string categoryName = this.TextBoxCategoryName.Text;
                JustTestDb context = new JustTestDb();
                Category category = new Category
                {
                    Name = categoryName
                };

                context.Categoriess.Add(category);
                context.SaveChanges();
                this.GridViewCategories.DataBind();
                this.TextBoxCategoryName.Text = " ";
                this.ButtonTogleAddCategory.Text = "New Category";
                this.PanelAddCategory.Visible = false;
            }
        }

        protected void ButtonTogleAddCategory_Click(object sender, EventArgs e)
        {
            if (this.PanelAddCategory.Visible)
            {
                this.PanelAddCategory.Visible = false;
                this.ButtonTogleAddCategory.Text = "New Category";
            }
            else
            {
                this.PanelAddCategory.Visible = true;
                this.ButtonTogleAddCategory.Text = "Cancel";
                this.TextBoxCategoryName.Text = "";
            }
        }

        protected void DropDownListCategoriesPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int categoriesPerPage = int.Parse(this.DropDownListCategoriesPerPage.SelectedValue);
            this.GridViewCategories.PageSize = categoriesPerPage;
        }
    }
}